package com.bootdo.lvyou.controller;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.lvyou.domain.SerchModel;
import com.bootdo.maputils.GpsJisuanUitils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.lvyou.domain.LvyouScenicMarker;
import com.bootdo.lvyou.service.LvyouScenicMarkerService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-10-09 16:35:40
 */
 
@Controller
@RequestMapping("/lvyou/lvyouScenicMarker")
public class LvyouScenicMarkerController extends BaseController{


	@Autowired
	private LvyouScenicMarkerService lvyouScenicMarkerService;
	
	@ModelAttribute
	public LvyouScenicMarker get(@RequestParam(required=false) Long id) {
		if (id != null){
			return lvyouScenicMarkerService.get(id);
		}else{
			return new LvyouScenicMarker();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/lvyou/lvyouScenicMarker','lvyou:lvyouScenicMarker:lvyouScenicMarker',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','lvyou:lvyouScenicMarker:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("lvyou:lvyouScenicMarker:lvyouScenicMarker")	
	String LvyouScenicMarker(LvyouScenicMarker lvyouScenicMarker,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou/lvyouScenicMarker/lvyouScenicMarker";
	}
	
	/*@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("lvyou:lvyouScenicMarker:lvyouScenicMarker")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<LvyouScenicMarker> lvyouScenicMarkerList = lvyouScenicMarkerService.list(query);
		int total = lvyouScenicMarkerService.count(query);
		PageUtils pageUtils = new PageUtils(lvyouScenicMarkerList, total);
		return pageUtils;
	}*/

	/**
	 * /lvyou/lvyouScenicMarker/list
	 * @param params
	 * @return
	 */
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("lvyou:lvyouScenicMarker:lvyouScenicMarker")
	public R list(@RequestParam Map<String, Object> params){
		List<LvyouScenicMarker> lvyouScenicMarkerList = lvyouScenicMarkerService.list(params);
		return R.ok().put("l", lvyouScenicMarkerList);
	}


	/**
	 * /lvyou/lvyouScenicMarker/listSerach
	 * @param params
	 * @return
	 */
	@ResponseBody
	@PostMapping("/listSerach")
	@RequiresPermissions("lvyou:lvyouScenicMarker:lvyouScenicMarker")
	public R listSerach(@RequestParam Map<String, Object> params){
		List<LvyouScenicMarker> lvyouScenicMarkerList = lvyouScenicMarkerService.list(params);
		List<SerchModel> resultList = new ArrayList<>();
		if(null != lvyouScenicMarkerList){
			lvyouScenicMarkerList.stream().forEach(item ->{
				SerchModel serchModel = new SerchModel();
				serchModel.setValue(item.getName());
				serchModel.setModel(item);
				resultList.add(serchModel);
			});
		}
		return R.ok().put("l", resultList);
	}


	
	@GetMapping("/add")
	@RequiresPermissions("lvyou:lvyouScenicMarker:edit")
	String add(LvyouScenicMarker lvyouScenicMarker,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou/lvyouScenicMarker/lvyouScenicMarkeredit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(LvyouScenicMarker lvyouScenicMarker) {
		return R.ok().put("i",lvyouScenicMarker);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("lvyou:lvyouScenicMarker:edit")
	public R save( LvyouScenicMarker lvyouScenicMarker){
	
		if(lvyouScenicMarkerService.save(lvyouScenicMarker)>0){
			return R.ok().put("i", lvyouScenicMarker);
		}
		return R.error();
	}


	@ResponseBody
	@PostMapping("/testCheck")
	public R testCheck(@RequestParam("refId")Long refId,
					   @RequestParam("type")Integer type,
		@RequestParam("lon")Double lon,@RequestParam("lat")Double lat
	){
		List<LvyouScenicMarker> list =  lvyouScenicMarkerService
				.list(MapBuild.getInstand().setKV("type",type)
						.setKV("refId", refId)
				);
		Point2D.Double my = new Point2D.Double(lon,lat);
		List<Point2D.Double> pts = new ArrayList<>();
		list.forEach(item ->{
			Point2D.Double p2d = new Point2D.Double();
			p2d.setLocation(item.getLon(),item.getLat());
			pts.add(p2d);
		});
		boolean b =  GpsJisuanUitils.isInPolygon(my,pts);
		return R.ok().put("i", b);
	}


	/**
	 * /lvyou/lvyouScenicMarker/celeanByTypeAndRefId
	 * 删除
	 */
	@PostMapping( "/celeanByTypeAndRefId")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouScenicMarker:edit")
	public R celeanByTypeAndRefId(LvyouScenicMarker lvyouScenicMarker){
		lvyouScenicMarkerService.removeByTypeAndRefId(lvyouScenicMarker);
		return R.ok();
	}


	/**
	 * 删除
	 */
	@PostMapping( "/celeanById")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouScenicMarker:edit")
	public R celeanById(Long id){
		if(lvyouScenicMarkerService.remove(id)>0){
			return R.ok();
		}
		return R.error();
	}

	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouScenicMarker:edit")
	public R remove( Long id){
		if(lvyouScenicMarkerService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("lvyou:lvyouScenicMarker:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		lvyouScenicMarkerService.batchRemove(ids);
		return R.ok();
	}
	
}